<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author FIRE
-->
<template>
  <PageWrapper :sidebarWidth="1200">
    <template #sidebar>
      <PageWrapper :sidebarWidth="450">
        <template #sidebar>
          <BasicTable @register="registerTable1" @row-click="handleTestDataChildRowClick1">
            <!-- 表格标题 -->
            <template #tableTitle>
              <Icon :icon="getTitle.icon" class="m-1 pr-1" />
              <span> {{ getTitle.value }} </span>
            </template>
          </BasicTable>
        </template>
        <BasicTable @register="registerTable" @row-click="handleTestDataChildRowClick">
          <!-- 表格标题 -->
          <template #tableTitle>
            <Icon :icon="getTitle.icon" class="m-1 pr-1" />
            <span> {{ getTitle.value }} </span>
          </template>
          <!-- 表格右上角自定义按钮（新增...） -->
          <template #toolbar>
            <a-popconfirm
              title="是否确认协同路线检查？"
              ok-text="是"
              cancel-text="否"
              placement="bottom"
              @confirm="handleCheck()"
            >
              <a-button type="primary" v-auth="'setting:price:edit'">
                <Icon icon="ant-design:video-camera-twotone" /> {{ t('协同路线检查') }}
              </a-button>
            </a-popconfirm>

            <a-popconfirm
              title="是否确认价格生效？"
              ok-text="是"
              cancel-text="否"
              placement="bottom"
              @confirm="handleStatus()"
            >
              <a-button type="primary" v-auth="'setting:price:edit'">
                <Icon icon="ant-design:check-outlined" /> {{ t('价格生效') }}
              </a-button>
            </a-popconfirm>
          </template>
        </BasicTable>
      </PageWrapper>
    </template>
    <ListView ref="viewList" @success="handleSuccess" :treeCode="treeCode" :invCode="invCode" />
  </PageWrapper>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsShopExtendShopExtendClsIndex',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { PageWrapper } from '/@/components/Page';
  import ListView from './list.vue';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { useMessage } from '/@/hooks/web/useMessage';
  import {
    valid,
    listEndCusData,
    treeData,
    updateCheck,
    listEndCusInvData,
  } from '/@/api/setting/price/wecharCusInvPriceRoute';
  const { t } = useI18n('setting.price.wecharCusInvPriceRoute');

  const treeCode = ref<string>('');
  const invCode = ref<string>('');

  const  viewList = ref();

  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: t('终端客户'),
  };

  const searchForm1: FormProps = {
    baseColProps: { lg: 24, md: 8 },
    labelWidth: 90,
    showAdvancedButton: false,
    schemas: [
      {
        label: t('终端客户'),
        field: 'endCusCode',
        // fieldLabel: 'endBasCus.name',
        component: 'TreeSelect',
        componentProps: {
          api: treeData,
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns1: BasicColumn[] = [
    {
      title: t('终端客户'),
      dataIndex: 'endCusCode',
      key: 'a.endCusCode',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('终端客户名称'),
      dataIndex: 'endBasCus.name',
      key: 'a.endBasCus.name',
      sorter: true,
      width: 180,
      align: 'left',
    },
  ];

  const searchForm: FormProps = {
    baseColProps: { lg: 12, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('终端客户'),
        field: 'endCusCode',
        // fieldLabel: 'endBasCus.name',
        component: 'TreeSelect',
        componentProps: {
          api: treeData,
          allowClear: true,
        },
      },
      {
        label: t('生效状态'),
        field: 'validStatus',
        component: 'Select',
        componentProps: {
          dictType: 'sys_valid',
          allowClear: true,
        },
      },
      {
        label: t('检验异常'),
        field: 'brouteErr',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('生效状态'),
      dataIndex: 'validStatus',
      key: 'a.validStatus',
      sorter: true,
      width: 120,
      align: 'center',
      dictType: 'sys_valid',
    },
    {
      title: t('检验异常'),
      dataIndex: 'brouteErr',
      key: 'a.brouteErr',
      sorter: true,
      width: 120,
      align: 'center',
      dictType: 'sys_yes_no',
    },
    // {
    //   title: t('终端客户'),
    //   dataIndex: 'endCusCode',
    //   key: 'a.endCusCode',
    //   sorter: true,
    //   width: 150,
    //   align: 'left',
    //   slot: 'firstColumn',
    // },
    // {
    //   title: t('终端客户名称'),
    //   dataIndex: 'endBasCus.name',
    //   key: 'a.endBasCus.name',
    //   sorter: true,
    //   width: 180,
    //   align: 'left',
    // },
    {
      title: t('产品编码'),
      dataIndex: 'invCode',
      key: 'a.inv_code',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('产品名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 180,
      align: 'left',
    },
    {
      title: t('协同路线'),
      dataIndex: 'croute',
      key: 'a.croute',
      sorter: true,
      width: 180,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('协同描述'),
      dataIndex: 'routeDesc',
      key: 'a.routeDesc',
      sorter: true,
      width: 230,
      align: 'left',
    },

    {
      title: t('异常提示'),
      dataIndex: 'routeErr',
      key: 'a.routeErr',
      sorter: true,
      width: 260,
      align: 'left',
    },
  ];

  const [registerTable1, { reload: reload1, getSelectRows: getSelectRows1 }] = useTable({
    api: listEndCusData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns1, //配置表格内容数组对象
    // actionColumn: actionColumn, //配置表格右边操作按钮
    formConfig: searchForm1, //配置表单内容
    showTableSetting: true, //表格右上角3个默认按钮
    useSearchForm: true, //表单是否展示
    canResize: true, //表格是否flex布局
    rowSelection: {
      type: 'radio',
    },
  });

  const [registerTable, { reload, getSelectRows, getForm }] = useTable({
    api: listEndCusInvData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns, //配置表格内容数组对象
    // actionColumn: actionColumn, //配置表格右边操作按钮
    formConfig: searchForm, //配置表单内容
    showTableSetting: true, //表格右上角3个默认按钮
    useSearchForm: true, //表单是否展示
    canResize: true, //表格是否flex布局
    rowSelection: {
      type: 'checkbox',
    },
  });
  async function handleTestDataChildRowClick1(record: Recordable) {
    treeCode.value = record.endCusCode;
    invCode.value = '';
    record.onEdit?.(true, false);
    await getForm().setFieldsValue({
      endCusCode: record.endCusCode,
    });
    reload();
  }
  async function handleTestDataChildRowClick(record: Recordable) {
    treeCode.value = record.endCusCode;
    invCode.value = record.invCode;

    record.onEdit?.(true, false);
    // reload2({
    //   searchInfo: { 'route.id': record.id },
    // });
  }
  function handleSuccess() {
    reload();
  }
  async function handleStatus() {
    if (getSelectRows().length) {
      try {
        let selIds = '';
        getSelectRows().forEach((item) => {
          selIds += item.id + ',';
        });
        const res = await valid({ selIds });
        showMessage(res.message);
      } catch (error: any) {
        console.log('error', error);
      } finally {
        handleSuccess();
        viewList.value.handleSuccess1()
      }
    } else {
      showMessage('请先选择数据！！');
    }
  }

  async function handleCheck() {
    if (getSelectRows().length) {
      try {
        let selIds = '';
        getSelectRows().forEach((item) => {
          selIds += item.id + ',';
        });
        const res = await updateCheck({ selIds });
        showMessage(res.message);
      } catch (error: any) {
        console.log('error', error);
      } finally {
        handleSuccess();
        viewList.value.handleSuccess1()
      }
    } else {
      showMessage('请先选择数据！！');
    }
  }
</script>
